//
//  MCLoginViewController.m
//  TLYL
//
//  Created by miaocai on 2017/6/1.
//  Copyright © 2017年 TLYL01. All rights reserved.
//



#import "MCLoginViewController.h"
#import "MCWaterRippleButton.h"
#import "MCMathUnits.h"
#import "MCMainTabBarController.h"

@interface MCLoginViewController ()
//用户名输入框
@property (nonatomic,weak) UITextField *userNameTF;
//密码输入框
@property (nonatomic,weak) UITextField *passWordTF;
//登录按钮
@property (nonatomic,weak) MCWaterRippleButton *loginBtn;
//底部ScrollView
@property (nonatomic,weak) UIScrollView *baseScrollV;

@end

@implementation MCLoginViewController

#pragma mark - life cycle
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setUpUI];
    [self addNotifation];

}

- (void)setUpUI{
    
    CGFloat padding = MC_REALVALUE(20);
    UIScrollView *baseScrolV = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:baseScrolV];
    self.baseScrollV = baseScrolV;
    self.view.backgroundColor = RGB(6, 34, 50);
    UIImageView *logoImageV = [[UIImageView alloc] init];

    logoImageV.image = [UIImage imageNamed:@"图层-5"];
    logoImageV.layer.cornerRadius = MC_REALVALUE(131) * 0.5;
    logoImageV.layer.masksToBounds = YES;
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [baseScrolV addSubview:bgImageView];
    [bgImageView addSubview:logoImageV];
    bgImageView.userInteractionEnabled = YES;
    bgImageView.image = [UIImage imageNamed:@"29381f30e924b899c83ff41c6d061d950a7bf697.jpg"];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewTapGestureRecognizer)];
    [bgImageView addGestureRecognizer:tap];
    [logoImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(baseScrolV);
        make.top.equalTo(baseScrolV).offset(MC_REALVALUE(35));
        make.height.width.equalTo(@(MC_REALVALUE(131)));
    }];
    logoImageV.backgroundColor = [UIColor redColor];
    UITextField *userNameTF = [[UITextField alloc] init];
    [bgImageView addSubview:userNameTF];
    
    userNameTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入用户名" attributes:@{NSForegroundColorAttributeName : MC_ColorWithAlpha(255, 255, 255, 0.95),NSFontAttributeName : [UIFont systemFontOfSize:MC_REALVALUE(17)]}];
    [userNameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgImageView.mas_left).offset(padding);
        make.right.equalTo(bgImageView.mas_right).offset(-padding);
        make.top.equalTo(logoImageV.mas_bottom).offset(MC_REALVALUE(100));
        make.height.equalTo(@(MC_REALVALUE(50)));
    }];
    userNameTF.textAlignment = NSTextAlignmentCenter;
    userNameTF.autocorrectionType = UITextAutocorrectionTypeNo;
    userNameTF.textColor = MC_ColorWithAlpha(255, 255, 255, 0.95);
    UIView *lineName = [[UIView alloc] init];
    [bgImageView addSubview:lineName];
    lineName.backgroundColor = MC_ColorWithAlpha(54, 118, 214, 128/255.0);
    [lineName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userNameTF.mas_bottom);
        make.left.equalTo(bgImageView).offset(MC_REALVALUE(40));
        make.right.equalTo(bgImageView).offset(-MC_REALVALUE(40));
        make.height.equalTo(@(1));
    }];
    self.userNameTF = userNameTF;
    UITextField *passWordTF = [[UITextField alloc] init];
    [bgImageView addSubview:passWordTF];
    passWordTF.autocorrectionType = UITextAutocorrectionTypeNo;
    passWordTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSForegroundColorAttributeName : MC_ColorWithAlpha(255, 255, 255, 0.95),NSFontAttributeName : [UIFont systemFontOfSize:MC_REALVALUE(17)]}];
    [passWordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(userNameTF.mas_left);
        make.right.equalTo(userNameTF.mas_right);
        make.top.equalTo(userNameTF.mas_bottom).offset(padding);
        make.height.equalTo(userNameTF.mas_height);
    }];
    passWordTF.textAlignment = NSTextAlignmentCenter;
    UIView *linePass = [[UIView alloc] init];
    passWordTF.textColor = MC_ColorWithAlpha(255, 255, 255, 0.95);
    [bgImageView addSubview:linePass];
    linePass.backgroundColor = MC_ColorWithAlpha(54, 118, 214, 128/255.0);
    [linePass mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passWordTF.mas_bottom);
        make.left.equalTo(bgImageView).offset(MC_REALVALUE(40));
        make.right.equalTo(bgImageView).offset(-MC_REALVALUE(40));
        make.height.equalTo(@(1));
    }];
    self.passWordTF = passWordTF;
    MCWaterRippleButton *loginBtn = [[MCWaterRippleButton alloc] init];
    [bgImageView addSubview:loginBtn];
    [loginBtn setTitle:@"登 录" forState:UIControlStateNormal];
    loginBtn.layer.cornerRadius = MC_REALVALUE(5);
    loginBtn.layer.masksToBounds = YES;
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(18)];
    [loginBtn addTarget:self action:@selector(loginButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    loginBtn.backgroundColor = RGB(54, 128, 211);
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(userNameTF.mas_left).offset(padding);
        make.right.equalTo(userNameTF.mas_right).offset(-padding);
        make.top.equalTo(passWordTF.mas_bottom).offset(MC_REALVALUE(66));
        make.height.equalTo(userNameTF.mas_height);
    }];
    
    
    UIButton *lxkfBtn = [[UIButton alloc] init];
    [bgImageView addSubview:lxkfBtn];
    [lxkfBtn setTitle:@"联系客服" forState:UIControlStateNormal];
    [lxkfBtn sizeToFit];
    lxkfBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    [lxkfBtn addTarget:self action:@selector(kfButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [lxkfBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bgImageView);
        make.bottom.equalTo(bgImageView.mas_bottom).offset(-MC_REALVALUE(25));
        
    }];
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

#pragma mark - system Notifacation
- (void)addNotifation{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillDisAppear:) name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark - touch event
- (void)kfButtonClick:(UIButton *)btn{
 #warning  TODO !!!
    NSLog(@"客服");
}
- (void)keyBoardWillAppear:(NSNotification *)notification{
    
    NSDictionary *dic = notification.userInfo;
    CGRect rect = [dic[@"UIKeyboardBoundsUserInfoKey"] CGRectValue];
    [UIView animateWithDuration:0.25 animations:^{
        self.baseScrollV.contentSize = CGSizeMake(G_SCREENWIDTH, MC_REALVALUE(510) - G_SCREENHEIGHT + rect.size.height);
        self.baseScrollV.contentOffset = CGPointMake(0, MC_REALVALUE(510) - G_SCREENHEIGHT + rect.size.height);
        
    }];
   
    }

- (void)keyBoardWillDisAppear:(NSNotification *)notification{

    self.baseScrollV.contentSize = CGSizeMake(G_SCREENWIDTH, G_SCREENHEIGHT);
    self.baseScrollV.contentOffset = CGPointMake(0, 0);
}

- (void)imageViewTapGestureRecognizer{
    
    [self.view endEditing:YES];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}

- (void)loginButtonClick:(UIButton *)btn{
    
    [self.view endEditing:YES];
    
    if([self.userNameTF.text  isEqualToString: @""]){
        
        return;
    }
    if ([self.passWordTF.text isEqualToString:@""]) {
        
        return;
    }
    
#warning FIX ME发送请求
    if ([self.userNameTF.text  isEqual: @"123"]
        &&[self.passWordTF.text isEqualToString:@"123"]) {
        NSLog(@"发送请求");
        
        [[NSUserDefaults standardUserDefaults] setObject:@"token" forKey:@"token"];
        
        [[[UIApplication sharedApplication] keyWindow] setRootViewController:[[MCMainTabBarController alloc] init]];
        
    }else{
        [self shakeAnimationForView:self.baseScrollV];
    }
    
}

- (void)shakeAnimationForView:(UIView *) view
{
    CALayer *viewLayer = view.layer;
    
    CGPoint position = viewLayer.position;
    
    CGPoint x = CGPointMake(position.x + 5, position.y);
    
    CGPoint y = CGPointMake(position.x - 5, position.y);
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    
    [animation setFromValue:[NSValue valueWithCGPoint:x]];
    
    [animation setToValue:[NSValue valueWithCGPoint:y]];
    
    [animation setAutoreverses:YES];
    
    [animation setDuration:.03];
    
    [animation setRepeatCount:4];

    [viewLayer addAnimation:animation forKey:nil];
    
}
@end
